package cn.tedu.pdfamily.product.controller;

import cn.tedu.pdfamily.product.pojo.dto.ProductAddNewDTO;
import cn.tedu.pdfamily.product.pojo.dto.ProductUpdateDTO;
import cn.tedu.pdfamily.product.pojo.vo.ProductListItemVO;
import cn.tedu.pdfamily.product.pojo.vo.ProductStandardVO;
import cn.tedu.pdfamily.product.service.IProductService;
import cn.tedu.pdfamily.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理商品相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/products")
@Api(tags = "02. 商品管理模块")
public class ProductController {
    @Autowired
    IProductService productService;

    public ProductController() {
        log.debug("创建控制器对象:ProductController");
    }

    // http://localhost:9080/products/add-new
    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid ProductAddNewDTO productAddNewDTO) {
        log.debug("开始处理【添加商品】的请求，参数：{}", productAddNewDTO);
        productService.addNew(productAddNewDTO);
        log.debug("添加商品成功！");
        return JsonResult.ok();
    }

    // http://localhost:9080/products/9527/delete
    @ApiOperation("根据id删除商品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除商品失败，尝试删除的商品的ID无效！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除商品】的请求，参数：{}", id);
        productService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/products/9527/update
    @ApiOperation("修改商品详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, ProductUpdateDTO productUpdateDTO) {
        log.debug("开始处理【修改商品详情】的请求，参数ID：{}, 新数据：{}", id, productUpdateDTO);
        productService.updateInfoById(id, productUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/products/9527
    @ApiOperation("根据id查询商品详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ProductStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询商品详情】的请求，参数：{}", id);
        ProductStandardVO product = productService.getStandardById(id);
        return JsonResult.ok(product);
    }

    // http://localhost:9080/products
    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ProductListItemVO>> list() {
        log.debug("开始处理【查询商品列表】的请求，无参数");
        List<ProductListItemVO> list = productService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:8080/admins/9527/enable
    @ApiOperation("启用")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1, message = "启用商品失败，尝试启用的商品的ID无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用商品】的请求，参数：{}", id);
        productService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:8080/admins/9527/disable
    @ApiOperation("禁用")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1, message = "禁用商品失败，尝试禁用的商品的ID无效！")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用商品】的请求，参数：{}", id);
        productService.setDisable(id);
        return JsonResult.ok();
    }
}
